package com.github.xuemingyu.springboot.jkfaststart.controller.admin;

import com.bidanet.bdcms.core.bean.AjaxCallBack;
import com.github.xuemingyu.springboot.jkfaststart.JkConfig;
import com.github.xuemingyu.springboot.jkfaststart.common.QueryDslTool;
import com.github.xuemingyu.springboot.jkfaststart.common.ShiroUtils;
import com.github.xuemingyu.springboot.jkfaststart.service.AdminUserService;
import lombok.extern.log4j.Log4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 后台主页
 *
 * @author xuejike
 */
@RequestMapping(JkConfig.ADMIN_PATH)
@Controller
@Log4j
public class HomeAdminController {
    @Autowired
    QueryDslTool queryDslTool;
    @Autowired
    AdminUserService adminUserService;

    @RequestMapping({"/", "/index"})
    public void index(Model model) {
        String nickName = ShiroUtils.getLoginUser().getNickName();
        model.addAttribute("nickName", nickName);
    }


    /**
     * 欢迎页统计
     *
     * @param model
     */
    @RequestMapping("/home")
    public void home(Model model, Long id) {
       /* List<Project> allByIsDelete = projectDao.findAllByIsDelete(false);
        ArrayList<JkNameValueData> projectList = new ArrayList<>();
        allByIsDelete.forEach(q -> {
            projectList.add(new JkNameValueDataImpl(q.getProjectName(), q.getId().toString()));
        });
        Project project = new Project();
        if (Objects.isNull(id)) {
            project = allByIsDelete.get(0);

        } else {
            project = projectDao.findOne(id);
        }
        List<String> pointByProjectId = tunnelMonitorDataService.getPointByProjectId(project.getId());
        List<DealDto> noDeal = tunnelMonitorDataService.getNoDealGroupByProjectId(project.getId());
        model.addAttribute("project", project);
        model.addAttribute("list", projectList);
        model.addAttribute("point", pointByProjectId);
        model.addAttribute("noDeal", noDeal);*/
    }

    @RequestMapping(value = "/public/login", method = RequestMethod.GET)
    public void login() {

    }

    @RequestMapping(value = "/public/login", method = RequestMethod.POST)
    public void loginPost(@ModelAttribute("username") String username,
                          @ModelAttribute("pwd") String pwd, Model model,
                          ServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
        try {
            subject.login(token);
        } catch (Exception ex) {
            model.addAttribute("msg", ex.getMessage());
        }

    }

    @RequestMapping(value = "/public/logoutMe")
    public String loginError(HttpSession session) {
        SecurityUtils.getSubject().logout();
        return "redirect:/admin/index";
    }

    @RequestMapping("/public/changePwd")
    public String changePwd() {
        return "/admin/resetPwd";
    }


    @RequestMapping(value = "/public/savePassWord", method = RequestMethod.POST)
    @ResponseBody
    public AjaxCallBack savePassWord(String pwd, String newPwd) {
        try {
            adminUserService.updatePwd(ShiroUtils.getLoginUser().getId(), pwd, newPwd);
        } catch (Exception e) {
            return AjaxCallBack.error(e.getMessage());
        }
        return AjaxCallBack.saveSuccess();
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String error(Exception ex) {
        ex.printStackTrace();
        return "error";
    }
}
